package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.auth.service.IBaseDataAuthService;
import com.penghaisoft.microwms.base.web.dataauth.dao.BaseUserDataAuthMapper;
import com.penghaisoft.microwms.base.web.factory.dao.BaseFactoryMapper;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseFactory;
import com.penghaisoft.microwms.common.base.model.BaseUser;
import com.penghaisoft.microwms.common.base.model.BaseUserDataAuth;
import com.penghaisoft.microwms.common.dto.BaseUserDataAuthForm;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxu
 * 数据权限服务
 */
@Service("baseDataAuthService")
public class BaseDataAuthServiceImpl implements IBaseDataAuthService {
	
	@Autowired
	private BaseUserDataAuthMapper baseUserDataAuthMapper;
	@Autowired
	private BaseFactoryMapper baseFactoryMapper;


	/**
	 * 查询某个用户的数据权限信息
	 */
	@Override
	public List<BaseUserDataAuth> queryUserDataAuth(BaseUser userInfo) {
//		要存储的用户的数据权限
		List<BaseUserDataAuth> userDataAuths = new ArrayList<BaseUserDataAuth>();
		userDataAuths = baseUserDataAuthMapper.queryUserDataAuth(userInfo);
		return userDataAuths;
	}


	/**
	* <p>Title: queryReportUserFactoryTypes</p> 
	* <p>Description:给穿梭框使用 </p> 
	* @param userInfo
	* @return 
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseDataAuthService#queryReportUserFactoryTypes(com.haier.cosmo.base.auth.model.BaseUser) 
	*/
	@Override
	public List<String> queryReportUserFactoryTypes(BaseUser userInfo) {
		List<String> factoryTypes = baseUserDataAuthMapper.queryUserDataAuthFactoryType(userInfo);
		return factoryTypes;
	}

	/** 
	* <p>Title: saveReportUserDataAuths</p> 
	* <p>Description: 保存报表用户的工厂类型</p> 
	* @param userDataAuths
	* @return 
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseDataAuthService#saveReportUserDataAuths(List)
	*/
	@Transactional
	@Override
	public Resp saveReportUserDataAuths(List<BaseUserDataAuth> userDataAuths) {
		Resp resp = new Resp();
		resp.setCode(Constant.SUCCESS);
		if (userDataAuths!=null&&userDataAuths.size()>0) {
			String loginName = userDataAuths.get(0).getLoginName();
//		1.删除旧有权限
			baseUserDataAuthMapper.deleteUserDataAuth(userDataAuths.get(0));
//		2.插入新权限
			List<BaseUserDataAuth> authList = new ArrayList<>();
			for (BaseUserDataAuth auth : userDataAuths) {
				String factoryType = auth.getFactoryType();
				List<BaseFactory> factories = baseFactoryMapper.queryFactoriesByType(factoryType);
				for (BaseFactory factory : factories) {
					BaseUserDataAuth auth2 = new BaseUserDataAuth();
					auth2.setLoginName(loginName);
					auth2.setFactoryType(factoryType);
					auth2.setFactoryCode(factory.getFactoryCode());
//					auth2.setFactoryName(factory.getFactoryName());
					authList.add(auth2);
				}
			}
			baseUserDataAuthMapper.insertReportUserFactories(authList);
		}
		return resp;
	}


	/**
	* <p>Title: saveBuzUserDataAuths</p>
	* <p>Description:与报表用户目前操作一样，函数分开是为了以后扩展，10有89会变动 </p>
	* @param dataAuths
	* @return
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseDataAuthService#saveBuzUserDataAuths(List)
	*/
	@Transactional
	@Override
	public Resp saveBuzUserDataAuths(List<BaseUserDataAuth> dataAuths) {
		Resp resp = new Resp();
		resp.setCode(Constant.SUCCESS);
		if (dataAuths!=null&&dataAuths.size()>0) {
			
//		1.删除旧有权限--业务用户与报表用户通用
			baseUserDataAuthMapper.deleteUserDataAuth(dataAuths.get(0));
//		2.插入新权限
			baseUserDataAuthMapper.insertBuzUserDataAuthBatch(dataAuths);
		}
		return resp;
	}

	

	/** 
	* <p>Title: queryBuzUserDataAuths</p> 
	* <p>Description: 查询业务用户的分类权限类型</p> 
	* @param param
	* @return 
	* @see com.haier.cosmo.base.auth.dataauth.service.IBaseDataAuthService#queryBuzUserDataAuths(com.haier.cosmo.base.auth.model.BaseUserDataAuth) 
	*/
	@Override
	public List<BaseUserDataAuth> queryBuzUserDataAuths(BaseUserDataAuth param) {
		return baseUserDataAuthMapper.queryBuzUserDataAuths(param);
	}

	/**
	 * @Description: 分页查询
	 * @param @param page
	 * @param @param rows
	 * @param @param condition
	 * @param @return   
	 * @return Pager<BaseUserDataauthType>  
	 * @author sun_yq
	 * @date 2017年9月11日 下午2:13:13
	 */
	@Override
	public Pager<BaseUserDataAuthForm> findListByCondition(int page, int rows, BaseUserDataAuthForm condition){
		Pager<BaseUserDataAuthForm> pager = new Pager<BaseUserDataAuthForm>();
		pager.setPage(page);
		pager.setRows(rows);
		condition.preQuery();
		List<BaseUserDataAuthForm> records = baseUserDataAuthMapper.queryFormList(pager,condition);
		long size = baseUserDataAuthMapper.queryFormCount(condition);
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}
}
